package com.mao.utils;

import java.io.*;

/**
 * @Description:
 * @Param:
 * @Thought:
 * @Author: Wenjie Mao
 * @create： 2022-11-16 15:05
 */
@Deprecated
/*
 used to split all json into separated record
 */
public class splitJsonIntoRecord {

    //index是当前记录的计数
    public static void splitIntoRecord(String srcUrl,String destUrl, int index){
        int record = 1;
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        FileOutputStream fileOutputStream = null;

        try {
            //创建最终的res文件

            //从文件夹中读取json文件
                System.out.println("=======================正在处理page"+index+"=========================");
                fileReader = new FileReader(srcUrl);
                bufferedReader = new BufferedReader(fileReader);
                byte[] bytes = new byte[1024];
                //循环读入，同时写入
                System.out.println("writing into res.json");

                StringBuilder sb = new StringBuilder();
                while (true) {
                    int ch = bufferedReader.read();
                    if (ch != -1) {
                        sb.append((char) ch);
                    } else {
                        break;
                    }
                }
                String s = sb.toString();
                int len = s.length();
                StringBuilder stringBuilder = new StringBuilder();

                //遍历json文件，将记录分别存
                for (int i = 0; i < len; i++) {
                    if(s.charAt(i) == '}'){
                        //找到
                        if(i + 1 < len && s.charAt(i + 1) == ','
                                && i + 2 < len && s.charAt(i + 2) == '{'){
                            boolean flag = false;
                            for (int j = i; j < i + 40; j++) {
                                if(j < len && s.charAt(j) == 'f'
                                        && j + 1 < len && s.charAt(j + 1) == 'u'
                                        && j + 2 < len && s.charAt(j + 2) == 'l'
                                        && j + 3 < len && s.charAt(j + 3) == 'l'){
                                    flag = true;
                                }
                            }
                            if(flag){
                                stringBuilder.append(s.charAt(i));

                                File file = new File(destUrl);
                                System.out.println("Page" + record+" handling");
                                record ++;
                                file.createNewFile();
                                fileOutputStream = new FileOutputStream(file);
                                fileOutputStream.write(stringBuilder.toString().getBytes());
                                fileOutputStream.flush();


                                //清除StringBuilder
                                stringBuilder.delete(0,stringBuilder.length());

                                i += 2;
                            }
                        }
                        else if(i == len - 1){
                            stringBuilder.append(s.charAt(i));

                            File file = new File(destUrl);
                            System.out.println("Page" + record+" handling");
                            record ++;
                            file.createNewFile();
                            fileOutputStream = new FileOutputStream(file);
                            fileOutputStream.write(stringBuilder.toString().getBytes());
                            fileOutputStream.flush();

                            //清除StringBuilder
                            stringBuilder.delete(0,stringBuilder.length());


                        }
                    }
                    stringBuilder.append(s.charAt(i));
                }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileReader.close();;
                bufferedReader.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
